package com.finace.money.common.util;

import com.finace.money.common.Constant;
import com.finace.money.data.bean.AccountInfoBean;
import com.finace.money.data.bean.BankContractBean;
import com.finace.money.data.bean.BankCradInfoBean;
import com.finace.money.data.bean.HistoryLineBean;
import com.finace.money.data.bean.MessageEndBean;
import com.finace.money.data.bean.MoneyRecordBean;
import com.finace.money.data.bean.QryOrderBean;
import com.finace.money.data.bean.QryPositionBean;
import com.finace.money.data.bean.QrySettleInfoBean;
import com.finace.money.data.bean.QryTradeBean;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.net.socket.ConnManager;
import com.finace.money.net.socket.PushReceiver;

import java.util.ArrayList;

/**
 * Created by zhangpeng on 2018/1/29.
 */

public class MessageParseUtil {

    private static ArrayList<HistoryLineBean> levelListOne = new ArrayList<>();
    private static ArrayList<HistoryLineBean> levelListFive = new ArrayList<>();
    private static ArrayList<HistoryLineBean> levelListFifteen = new ArrayList<>();
    private static ArrayList<HistoryLineBean> levelListHalfHour = new ArrayList<>();
    private static ArrayList<HistoryLineBean> levelListHour = new ArrayList<>();
    private static ArrayList<HistoryLineBean> levelListDay = new ArrayList<>();
    private static ArrayList<StatusListBean> statusLists = new ArrayList();
    private static ArrayList<QryPositionBean> mQryPositionLists = new ArrayList();
    private static ArrayList<QryOrderBean> mQryOrderLists = new ArrayList();
    private static ArrayList<QryTradeBean> mQryTradeLists = new ArrayList();
    private static ArrayList<BankContractBean> mBanks = new ArrayList();
    private static ArrayList<MoneyRecordBean> mMoneyIns = new ArrayList();
    private static ArrayList<MoneyRecordBean> mMoneyOuts = new ArrayList();
    private static ArrayList<String> mSettleInfoStrs = new ArrayList();

    public static void parseData(String message, ConnManager.ConnectionListener mListener) {
        ACache aCache = ACache.get(UIUtil.getContext());
//        if (!message.contains("Tick")) {
//            LogUtil.d("connect", "onMessage: " + "接收成功" + message.toString());
//        }
        LogUtil.d("connect", "onMessage: " + "接收成功" + message.toString());
        if (message.contains("QueryCandleResponse")) {
            addData(message);
        } else if (message.contains("CandleNotify")) {
            updateData(message);
            mListener.pushData(message);
        } else if (message.contains("QueryProductResponse")) {
            StatusListBean bean = JsonUtils.fromJsonToBaseConn(message, StatusListBean.class);
            statusLists.add(bean);
        } else if (message.contains("QUERY_END")) {
            MessageEndBean bean = JsonUtils.fromJsonToBaseConn(message, MessageEndBean.class);
            if (bean.getM_CorrespondingRequestType() == 11) {
                aCache.put(PushReceiver.ACTION_LIST, statusLists);
                //订阅实时行情
                MessageSendUtil.RegisterSymbols();
//                statusLists.clear();
            } else if (bean.getM_CorrespondingRequestType() == 20) {
                saveData(bean.getM_RequestID());
            }
            mListener.pushData(message);
        } else if (message.contains("QryAccountInfoResponse")) {//QryAccountResponse
            AccountInfoBean bean = JsonUtils.fromJsonToBaseConn(message, AccountInfoBean.class);
            aCache.put(Constant.ACCOUNT_INFO, bean);
            mListener.pushData(Constant.ACCOUNT_INFO);
        } else if (message.contains("QryPositionResponse")) {
            QryPositionBean qryPositionBean = JsonUtils.fromJsonToBaseConn(message, QryPositionBean.class);
            if (qryPositionBean.getPosition().getPosition() != 0) {
                mQryPositionLists.add(qryPositionBean);
            }
            if (qryPositionBean.isIs_last()) {
                aCache.put(PushReceiver.QRY_POSITION_END, mQryPositionLists);
                mQryPositionLists.clear();
                mListener.pushData(PushReceiver.QRY_POSITION_END);
            }
        } else if (message.contains("QryOrderResponse")) {
            QryOrderBean qryOrderBean = JsonUtils.fromJsonToBaseConn(message, QryOrderBean.class);
            if (StringUtil.isNotNullString(qryOrderBean.getOrder().getSymbol())) {
                mQryOrderLists.add(qryOrderBean);
            }
            if (qryOrderBean.isIs_last()) {
                aCache.put(PushReceiver.QRY_ORDER_END, mQryOrderLists);
                mQryOrderLists.clear();
                mListener.pushData(PushReceiver.QRY_ORDER_END);
            }
        } else if (message.contains("QryTradeResponse")) {
            QryTradeBean qryTradeBean = JsonUtils.fromJsonToBaseConn(message, QryTradeBean.class);
            if (StringUtil.isNotNullString(qryTradeBean.getTrade().getSymbol())) {
                mQryTradeLists.add(qryTradeBean);
            }
            if (qryTradeBean.isIs_last()) {
                aCache.put(PushReceiver.QRY_TRADE_END, mQryTradeLists);
                mListener.pushData(PushReceiver.QRY_TRADE_END);
                mQryTradeLists.clear();
            }
        } else if (message.contains("QrySettleInfoResponse")) {
            QrySettleInfoBean qrySettleInfoBean = JsonUtils.fromJsonToBaseConn(message, QrySettleInfoBean.class);
            mSettleInfoStrs.add(qrySettleInfoBean.getSettlementContent());
            if (qrySettleInfoBean.isIs_last()) {
                aCache.put(PushReceiver.QRY_SETTLEINFO_END, mSettleInfoStrs);
                mListener.pushData(PushReceiver.QRY_SETTLEINFO_END);
                mSettleInfoStrs.clear();
            }
        } else if (message.contains("TickSnapshot")) {
            TickBean tickBean = JsonUtils.fromJsonToBaseConn(message, TickBean.class);
            for (StatusListBean statusListBean : statusLists) {
                if (statusListBean.getM_Body().getM_ProductID().equals(tickBean.getSymbol())) {
                    statusListBean.getM_Body().setTickbean(tickBean);
                    break;
                }
            }
            aCache.put(PushReceiver.ACTION_LIST, statusLists);
            mListener.pushData(message);
        } else if (message.contains("OrderNotify")) {
            QryOrderBean qryOrderBean = JsonUtils.fromJsonToBaseConn(message, QryOrderBean.class);
            ArrayList<QryOrderBean> orders = (ArrayList<QryOrderBean>) aCache.getAsObject(PushReceiver.QRY_ORDER_END);
            int have = 0;
            if (orders.size() > 0) {
                for (QryOrderBean qyyO : orders) {
                    if (qyyO.getOrder().getId() == qryOrderBean.getOrder().getId()) {
                        qyyO.setOrder(qryOrderBean.getOrder());
                        have++;
                        break;
                    }
                }
            }
            if (have == 0) {
                orders.add(qryOrderBean);
            }

            aCache.put(PushReceiver.QRY_ORDER_END, orders);
            mListener.pushData(PushReceiver.QRY_ORDER_END);
        } else if (message.contains("TradeNotify")) {
            QryTradeBean qryTradeBean = JsonUtils.fromJsonToBaseConn(message, QryTradeBean.class);
            ArrayList<QryTradeBean> trades = (ArrayList<QryTradeBean>) aCache.getAsObject(PushReceiver.QRY_TRADE_END);
            trades.add(qryTradeBean);
            aCache.put(PushReceiver.QRY_TRADE_END, trades);
            mListener.pushData(PushReceiver.QRY_TRADE_END);
        } else if (message.contains("PositionNotify")) {
            QryPositionBean qryPositionBean = JsonUtils.fromJsonToBaseConn(message, QryPositionBean.class);
            ArrayList<QryPositionBean> positions = (ArrayList<QryPositionBean>) aCache.getAsObject(PushReceiver.QRY_POSITION_END);
            int have = 0;
            if (positions.size() > 0) {
                for (QryPositionBean qyyQ : positions) {
                    if (qyyQ.getPosition().getSymbol().equals(qryPositionBean.getPosition().getSymbol())
                            && qyyQ.getPosition().isSide() == qryPositionBean.getPosition().isSide()) {
                        qyyQ.setPosition(qryPositionBean.getPosition());
                        have++;
                    }
                }
            }
            if (have == 0) {
                positions.add(qryPositionBean);
            }
            aCache.put(PushReceiver.QRY_POSITION_END, positions);
            mListener.pushData(PushReceiver.QRY_POSITION_END);
        } else if (message.contains("LogicHeartbeat")) {
            aCache.put(Constant.LOGIN_HEART, Constant.LOGIN_HEART);
        } else if (message.contains("QryInvestorResponse")) {
            BankCradInfoBean bankCradInfoBean = JsonUtils.fromJsonToBaseConn(message, BankCradInfoBean.class);
            aCache.put(Constant.BANK_CARD_INFO, bankCradInfoBean);
            mListener.pushData(message);
        } else if (message.contains("QryContractBankResponse")) {
            saveBank(message);
        } else if (message.contains("QryTransferSerialResponse")) {
            saveMoneyRecord(message);
            mListener.pushData(message);
        } else {
            mListener.pushData(message);
        }
    }

    private static void saveMoneyRecord(String message) {

        ACache aCache = ACache.get(UIUtil.getContext());
        MoneyRecordBean bean = JsonUtils.fromJsonToBaseConn(message, MoneyRecordBean.class);
        if (bean.getAmount() > 0) {
            mMoneyIns.add(bean);
            if (bean.isIs_last()) {
                aCache.put(Constant.MONEY_IN, mMoneyIns);
                mMoneyIns.clear();
            }
        } else {
            mMoneyOuts.add(bean);
            if (bean.isIs_last()) {
                aCache.put(Constant.MONEY_OUT, mMoneyOuts);
                mMoneyOuts.clear();
            }
        }
    }

    /**
     * 保存银行信息
     *
     * @param message
     */
    private static void saveBank(String message) {
        BankContractBean bank = JsonUtils.fromJsonToBaseConn(message, BankContractBean.class);
        mBanks.add(bank);
        if (bank.isIs_last()) {
            ACache aCache = ACache.get(UIUtil.getContext());
            aCache.put(Constant.BANK_LIST, mBanks);
            mBanks.clear();
        }
    }

    private static void saveData(int m_requestID) {

        ACache aCache = ACache.get(UIUtil.getContext());
        int level = UIUtil.getRandomLevel(m_requestID);
        String saveKey;
        switch (level) {
            case 1:
                if (levelListOne.size() > 0) {
                    saveKey = levelListOne.get(0).getM_ProductID() + level;
                    aCache.put(saveKey, levelListOne);
                    levelListOne.clear();
                }
                break;
            case 2:
                if (levelListFive.size() > 0) {
                    saveKey = levelListFive.get(0).getM_ProductID() + level;
                    aCache.put(saveKey, levelListFive);
                    levelListFive.clear();
                }
                break;
            case 3:
                if (levelListFifteen.size() > 0) {
                    saveKey = levelListFifteen.get(0).getM_ProductID() + level;
                    aCache.put(saveKey, levelListFifteen);
                    levelListFifteen.clear();
                }
                break;
            case 4:
                if (levelListHalfHour.size() > 0) {
                    saveKey = levelListHalfHour.get(0).getM_ProductID() + level;
                    aCache.put(saveKey, levelListHalfHour);
                    levelListHalfHour.clear();
                }
                break;
            case 5:
                if (levelListHour.size() > 0) {
                    saveKey = levelListHour.get(0).getM_ProductID() + level;
                    aCache.put(saveKey, levelListHour);
                    levelListHour.clear();
                }
                break;
            case 6:
                if (levelListDay.size() > 0) {
                    saveKey = levelListDay.get(0).getM_ProductID() + level;
                    aCache.put(saveKey, levelListDay);
                    levelListDay.clear();
                }
                break;
        }
    }

    private static void updateData(String message) {

        ACache aCache = ACache.get(UIUtil.getContext());
        HistoryLineBean newLineBean = JsonUtils.fromJsonToBaseConn(message, HistoryLineBean.class);
        ArrayList<HistoryLineBean> cacheData = (ArrayList<HistoryLineBean>) aCache.getAsObject(newLineBean.getM_ProductID() + newLineBean.getM_Level());
        cacheData.add(newLineBean);
        aCache.put(newLineBean.getM_ProductID() + newLineBean.getM_Level(), cacheData);
    }

    private static void addData(String message) {

        HistoryLineBean historyLineBean = JsonUtils.fromJsonToBaseConn(message, HistoryLineBean.class);
        switch (historyLineBean.getM_Level()) {
            case 1:
                levelListOne.add(historyLineBean);
                break;
            case 2:
                levelListFive.add(historyLineBean);
                break;
            case 3:
                levelListFifteen.add(historyLineBean);
                break;
            case 4:
                levelListHalfHour.add(historyLineBean);
                break;
            case 5:
                levelListHour.add(historyLineBean);
                break;
            case 6:
                levelListDay.add(historyLineBean);
                break;
        }
    }
}
